<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pasien_data extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('m_pasien_data');
		$this->load->library('breadcrumb');	
	}

	public function index(){
		$this->rule->type('R');
		$this->load->library('paging'); 

		//Menghapus session pencarian
		$this->session->unset_userdata('keyword');

		$config['sql'] = $this->m_pasien_data->get_all();
		$config['per_page'] = 6;
		$data = $this->paging->create_pagination($config);
		$judul = "Data Pasien";
		$data['judul'] = $judul;
		$this->layout->set_title($judul);
		$this->layout->set_desc("lorem ipsum");

		#breadcrumbs
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Pasien', site_url('pasien_data#'));
		$this->breadcrumb->add_crumb($judul);

		//untuk motong kalimat
		$this->load->helper('trim_text');
		//untuk format tanggal
		$this->load->helper('id_date');

		$this->layout->theme('apotek', 'pasien_data/index', $data);
	}

	public function search(){
		$this->rule->type('R');

		if(isset($_POST['keyword'])){
			$data['keyword'] = $this->input->post('keyword');
			$this->session->set_userdata('keyword', $data['keyword']);
		} else {
			$data['keyword'] = $this->session->userdata('keyword');
		}

		#pagination
		$this->load->library('paging');
		$config['sql'] = $this->m_pasien_data->search($data['keyword']);
		$config['per_page'] = 5;
		$data = $this->paging->create_pagination($config);

		$judul = 'Pencarian Data Pasien "' . $this->session->userdata('keyword') . '"';
    $data['judul']  = 'Pencarian Data Pasien " <font color="#FC771F">' . $this->session->userdata('keyword') . '</font> "';

    $this->layout->set_title($judul);
    $this->layout->set_desc('Made with love by Raksa Abadi Informatika');

    #import css + js
		$this->layout->set_include('css', 'themes/apotek/css/path-to-file.css');
		$this->layout->set_include('js', 'themes/apotek/css/path-to-file.js');
		$this->layout->set_include('js', 'themes/apotek/css/path-to-file.js', 'header');
	
		#breadcrumbs
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Pasien', site_url('pasien_data#'));
		$this->breadcrumb->add_crumb('Data Pasien', site_url('pasien_data'));
		$this->breadcrumb->add_crumb($judul);

		$this->load->helper('trim_text');
		$this->load->helper('id_date');

		$this->layout->theme('apotek','pasien_data/index', $data);
	}

	public function add(){
		$this->rule->type('C');
		//Memanggil helper
		$this->load->helper('id_date');
		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_pasien_data->store($input);

			redirect('pasien_data', 'refresh');
		}else{
			$judul = 'Tambah Data Pasien';
			$data['judul']  = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');

	    $this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
			$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
			$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

	    #breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Pasien', site_url('pasien_data#'));
			$this->breadcrumb->add_crumb('Data Pasien', site_url('pasien_data'));
			$this->breadcrumb->add_crumb($judul);

			$this->layout->theme('apotek','pasien_data/add', $data);
		}
	}

	public function edit($id){
		$this->rule->type('U');
		//Memanggil helper
		$this->load->helper('id_date');
		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_pasien_data->update($input);

			redirect('pasien_data', 'refresh');
		}else{
			$this->load->helper('cek_id');
			if(!cek_id($id, 'KdPasien', 'pasien')){
				show_404();
			}

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');

			$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
			$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
			$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');
			
			$judul = 'Edit Data Pasien';
			$data['judul'] = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

  		#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Pasien', site_url('pasien_data#'));
			$this->breadcrumb->add_crumb('Data Pasien', site_url('pasien_data'));
			$this->breadcrumb->add_crumb($judul);

			$data['data'] = $this->m_pasien_data->get_by_id($id);

			$this->layout->theme('apotek','pasien_data/edit', $data);
		}
	}

	public function delete($id){
		$this->rule->type('D');

		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdPasien', 'pasien')){
			set_flash('danger','Ups, Data Pasien tidak ditemukan');
  		redirect('pasien_data','refresh');
		}

		$this->m_pasien_data->destroy($id);

		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	public function excel($keyword = ""){
		$this->rule->type('R');
		
		#load our new PHPExcel library
  	$this->load->library('excel');
		#activate worksheet number 1
  	$this->excel->setActiveSheetIndex(0);
		#name the worksheet
  	$this->excel->getActiveSheet()->setTitle('Data Pasien');
		#set cell A1 content with some text
		if($keyword != "" && $keyword != " "){
  		$this->excel->getActiveSheet()->setCellValue('A1', 'Data Pasien Pencarian Kata Kunci "'.$keyword.'" '.site_title());
  	}else{
  		$this->excel->getActiveSheet()->setCellValue('A1', 'Data Pasien '.site_title());
  	}	

  	$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
  	$this->excel->getActiveSheet()->setCellValue('B2', 'Kode Pasien');
  	$this->excel->getActiveSheet()->setCellValue('C2', 'Nama Pasien');
  	$this->excel->getActiveSheet()->setCellValue('D2', 'Tanggal Lahir');
  	$this->excel->getActiveSheet()->setCellValue('E2', 'Jenis Kelamin');
  	$this->excel->getActiveSheet()->setCellValue('F2', 'No.Telp');
  	$this->excel->getActiveSheet()->setCellValue('G2', 'Alamat');
		#change the font size
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		#make the font become bold
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		#merge cell A1 until D1
  	$this->excel->getActiveSheet()->mergeCells('A1:G1');
		#set aligment to center for that merged cell (A1 to D1)
  	$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		#set style
  	$TitleStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
           // 'color' => array('rgb' => '00994C') // hijau tua
            ),
  		'font'  => array(
  			'bold'  => true,
  			//'color' => array('rgb' => '000000'),
  			'size'  => 12,
  			'name'  => 'calibri'
  			)
  		);

  	$HeaderStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => '32CD32') // hijau
            ),
  		'font'  => array(
  			'bold'  => true,
  			'size'  => 12,
  			'name'  => 'calibri'
  			)
  		);

  	$ContentStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFF00') // kuning
            ),
  		'font'  => array(
  			'size'  => 12,
  			'name'  => 'calibri'
  			)
  		);

  	$this->excel->getActiveSheet()->getStyle('A1:G1')->applyFromArray($TitleStyle);
  	$this->excel->getActiveSheet()->getStyle('A2:G2')->applyFromArray($HeaderStyle);
		//isi
		if($keyword != "" && $keyword != " "){
			$list_data = $this->m_pasien_data->export_by($keyword);
		}else{
			$list_data = $this->m_pasien_data->export();
		}
		
  	$cols = 3;
  	$num = 1;
  	foreach($list_data as $row){
  		$col = $cols++;
  		$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
  		$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['KdPasien']);
  		$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['Nama']);
  		$this->excel->getActiveSheet()->setCellValue('D' . $col, $row['TanggalLahir']);
  		$this->excel->getActiveSheet()->setCellValue('E' . $col, $row['JKel']);
  		$this->excel->getActiveSheet()->setCellValue('F' . $col, $row['Telp']);
  		$this->excel->getActiveSheet()->setCellValue('G' . $col, $row['Alamat']);
  	}

		#warna
  	$cols2 = 3;
  	foreach($list_data as $row){
  		$col2 = $cols2++;
  		$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('E' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('F' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('G' . $col2)->applyFromArray($ContentStyle);
  	}

  	$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);


		$filename='data pasien.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

}

/* End of file pasien_data.php */
/* Location: ./application/controllers/pasien_data.php */